# -*- coding:utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from wenlincms.conf import settings
from wenlincms.core.admin import DisplayableAdmin, OwnableAdmin

from wlapps.article.models import ArticlePost, ArticleCategory
from wlapps.utils.common_a import make_status_online, make_status_offline


class ArticlePostAdmin(DisplayableAdmin, OwnableAdmin):
    """
    Admin class for article posts.
    """
    fieldsets = ((None, {
        "fields": ["title", "status", "categories", "allow_comments", "featured_image", "content",
                   "view_count"],
    }), (_("Meta data"), {"fields": ["slug", "description", "keywords", ],
                          "classes": ("collapse-open",)}),)
    list_display = ["id", "title", "user", "status", "created", "admin_link"]
    list_filter = ("status", "categories",)
    list_editable = ("status",)
    filter_horizontal = ("categories",)
    readonly_fields = ("view_count",)
    actions = (make_status_online, make_status_offline)

    def save_form(self, request, form, change):
        """
        Super class ordering is important here - user must get saved first.
        """
        OwnableAdmin.save_form(self, request, form, change)
        return DisplayableAdmin.save_form(self, request, form, change)


class ArticleCategoryAdmin(admin.ModelAdmin):
    """
    Admin class for article categories. Hides itself from the admin menu
    unless explicitly specified.
    """

    fieldsets = ((None, {"fields": ("title", "slug")}),)

    def in_menu(self):
        """
        Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``.
        """
        for (name, items) in settings.ADMIN_MENU_ORDER:
            if "article.ArticleCategory" in items:
                return True
        return False


admin.site.register(ArticlePost, ArticlePostAdmin)
admin.site.register(ArticleCategory, ArticleCategoryAdmin)
